package zhihui.mobile.ui.utils;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.zgm.utils.SharedPreferencesUtis;
import com.zgm.utils.ui.CustomCheckTextView;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import zhihui.mobile.R;
import zhihui.mobile.app.MyApplication;
import zhihui.mobile.bean.address.Area;
import zhihui.mobile.bean.address.City;
import zhihui.mobile.bean.address.Province;
import zhihui.mobile.bean.address.UiData;
import zhihui.mobile.bean.shop.address.ShoppingAddress;
import zhihui.mobile.bean.shop.sales.CouponInfo;
import zhihui.mobile.bean.user.UserInfo;
import zhihui.mobile.config.Config;
import zhihui.mobile.interfaces.share.IShareListener;
import zhihui.mobile.manager.user.UserManager;
import zhihui.mobile.ui.address.adapter.AreaAdapter;
import zhihui.mobile.ui.login.LoginActivity;
import zhihui.mobile.ui.shop.address.ShopAddressAddActivity;
import zhihui.mobile.ui.shop.coupon.CouponAdapter;

/**
 * Created by 张公铭 on 2017/8/8 0008.
 */

public class UiUtils
{
    public interface IAddressSelectListener
    {
        void updateText(String name, int type);

        void setProv(String name, String code);

        void setCity(String name, String code);

        void setArea(String name, String code);
    }

    public interface ISelectAddressListener
    {
        void refreshView(ShoppingAddress address);
    }

    public interface ISelectCouponListener
    {
        void refreshView(CouponInfo couponInfo);
    }

    public interface IPayListener
    {
        void weixinPay();

        void ilPay();
    }


    public static Dialog getShopAddressDalig(final Context context, final List<ShoppingAddress> list, final ISelectAddressListener listener)
    {
        final Dialog dialog = new Dialog(context, R.style.ActionSheetDialogStyle);
        View view = LayoutInflater.from(context).inflate(R.layout.shop_address_dialog, null);
        ImageView iv_close = (ImageView) view.findViewById(R.id.iv_address_dialog_close);
        iv_close.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                dialog.dismiss();
            }
        });
        ListView lv_content = (ListView) view.findViewById(R.id.lv_shop_address_dialog_address);
        lv_content.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        ListAdapter listAdapter = new ArrayAdapter<ShoppingAddress>(context, R.layout.shop_address_dialog_item, list)
        {
            @Override
            public View getView(int position, View convertView, ViewGroup parent)
            {
                CustomCheckTextView view = null;
                if (convertView == null)
                {
                    view = new CustomCheckTextView(context);
                } else
                {
                    view = (CustomCheckTextView) convertView;
                }
                view.setTitle(list.get(position).getAddress());
                view.setChecked(list.get(position).getIsdefault() == 1);
                return view;
            }
        };
        lv_content.setAdapter(listAdapter);
        lv_content.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            {
                listener.refreshView(list.get(position));
                dialog.dismiss();
            }
        });

        TextView tv_add = (TextView) view.findViewById(R.id.tv_shop_address_dialog_add);
        tv_add.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                Intent intent = new Intent(context, ShopAddressAddActivity.class);
                context.startActivity(intent);
            }
        });

        //布局给Dialog
        dialog.setContentView(view);
        //获取当前Activity窗体
        Window window = dialog.getWindow();
        //设置dialog 从底部弹出
        window.setGravity(Gravity.BOTTOM);
        window.getDecorView().setPadding(0, 0, 0, 0);
        //获得窗体属性
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.y = 0;
        window.setAttributes(params);
        return dialog;
    }

    public static void showShoppingAddress(final Context context, final List<ShoppingAddress> list, final ISelectAddressListener listener)
    {
        getShopAddressDalig(context, list, listener).show();
    }

    /**
     * 优惠券dialog
     */
    public static void showCouponDialog(Context context, final List<CouponInfo> list, final ISelectCouponListener listener)
    {
        final Dialog dialog = new Dialog(context, R.style.ActionSheetDialogStyle);
        View view = LayoutInflater.from(context).inflate(R.layout.shop_coupon_dialog, null);
        ImageView iv_close = (ImageView) view.findViewById(R.id.iv_coupon_dialog_close);
        iv_close.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                dialog.dismiss();
            }
        });
        ListView lv_content = (ListView) view.findViewById(R.id.lv_shop_coupon_dialog_content);
        CouponAdapter adapter = new CouponAdapter(context);
        adapter.setList(list);
        lv_content.setAdapter(adapter);
        lv_content.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            {
                listener.refreshView(list.get(position));
                dialog.dismiss();
            }
        });

        //布局给Dialog
        dialog.setContentView(view);
        //获取当前Activity窗体
        Window window = dialog.getWindow();
        //设置dialog 从底部弹出
        window.setGravity(Gravity.BOTTOM);
        window.getDecorView().setPadding(0, 0, 0, 0);
        //获得窗体属性
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.y = 0;
        window.setAttributes(params);
        dialog.show();
    }

    public static void showPayDialog(final Context context, int paymoney, final IPayListener listener)
    {
        final Dialog dialog = new Dialog(context, R.style.ActionSheetDialogStyle);
        View view = LayoutInflater.from(context).inflate(R.layout.shop_pay_dialog, null);
        ImageView iv_close = (ImageView) view.findViewById(R.id.iv_pay_dialog_close);
        TextView tv_money = (TextView) view.findViewById(R.id.tv_pay_dialog_money);
        tv_money.setText("￥" + ((double) paymoney / 100));
        iv_close.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                dialog.dismiss();
            }
        });
        final CheckBox cb_ilpay = (CheckBox) view.findViewById(R.id.cb_pay_dialog_ilpay);
        final CheckBox cb_weixin = (CheckBox) view.findViewById(R.id.cb_pay_dialog_weixin);
        cb_ilpay.setChecked(true);
        cb_weixin.setChecked(false);
        cb_ilpay.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
        {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
            {
                if (isChecked)
                {
                    cb_weixin.setChecked(false);
                }
            }
        });

        cb_weixin.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
        {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
            {
                if (isChecked)
                {
                    cb_ilpay.setChecked(false);
                }
            }
        });


        RelativeLayout rl_ilpay = (RelativeLayout) view.findViewById(R.id.rl_pay_dialog_ilpay);
        rl_ilpay.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                cb_weixin.setChecked(false);
            }
        });

        RelativeLayout rl_weixin = (RelativeLayout) view.findViewById(R.id.rl_pay_dialog_weixin);
        rl_weixin.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                cb_ilpay.setChecked(false);
            }
        });

        TextView tv_pay = (TextView) view.findViewById(R.id.tv_pay_dialog_pay);
        tv_pay.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                if (!cb_weixin.isChecked() && !cb_ilpay.isChecked())
                {
                    Toast.makeText(context, "请选择支付方式！", Toast.LENGTH_SHORT);
                    return;
                }
                if (cb_weixin.isChecked())
                {
                    listener.weixinPay();
                    dialog.dismiss();
                    return;
                }
                if (cb_ilpay.isChecked())
                {
                    listener.ilPay();
                    dialog.dismiss();
                    return;
                }
            }
        });

        //布局给Dialog
        dialog.setContentView(view);
        //获取当前Activity窗体
        Window window = dialog.getWindow();
        //设置dialog 从底部弹出
        window.setGravity(Gravity.BOTTOM);
        window.getDecorView().setPadding(0, 0, 0, 0);
        //获得窗体属性
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.y = 0;
        window.setAttributes(params);
        dialog.show();
    }

    public static void showAddressDialog(Context context, final List<Province> list, final IAddressSelectListener listener)
    {
        final Dialog dialog = new Dialog(context, R.style.ActionSheetDialogStyle);
        View view = LayoutInflater.from(context).inflate(R.layout.address_dialog, null);
        ListView lv_province = (ListView) view.findViewById(R.id.lv_province);
        final AreaAdapter provinceAdapter = new AreaAdapter(context);
        provinceAdapter.setmList(list);
        lv_province.setAdapter(provinceAdapter);
        lv_province.setSelection(0);

        final List<City> cityList = new ArrayList<>();
        cityList.addAll(list.get(0).getCity());
        ListView lv_city = (ListView) view.findViewById(R.id.lv_city);
        final AreaAdapter cityAdapter = new AreaAdapter(context);
        cityAdapter.setmList(cityList);
        lv_city.setAdapter(cityAdapter);
        lv_city.setSelection(0);

        final List<Area> areaList = new ArrayList<>();
        areaList.addAll(cityList.get(0).getArea());
        ListView lv_area = (ListView) view.findViewById(R.id.lv_area);
        final AreaAdapter areaAdapter = new AreaAdapter(context);
        areaAdapter.setmList(areaList);
        lv_area.setAdapter(areaAdapter);
        lv_area.setSelection(0);

        lv_province.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            {
                cityList.clear();
                cityList.addAll(list.get(position).getCity());
                cityAdapter.setmList(cityList);
                cityAdapter.notifyDataSetChanged();

                areaAdapter.setmList(new ArrayList<UiData>());
                areaAdapter.notifyDataSetChanged();
                listener.updateText(list.get(position).getName(), 1);
                listener.setProv(list.get(position).getName(), list.get(position).getId());
            }
        });

        lv_city.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            {
                areaList.clear();
                areaList.addAll(cityList.get(position).getArea());
                areaAdapter.setmList(areaList);
                areaAdapter.notifyDataSetChanged();
                listener.updateText(cityList.get(position).getName(), 2);
                listener.setCity(cityList.get(position).getName(), cityList.get(position).getId());
            }
        });

        lv_area.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            {
                listener.updateText(areaList.get(position).getName(), 3);
                listener.setArea(areaList.get(position).getName(), areaList.get(position).getId());
                dialog.dismiss();
                dialog.cancel();
            }
        });

        //布局给Dialog
        dialog.setContentView(view);
        //获取当前Activity窗体
        Window window = dialog.getWindow();
        //设置dialog 从底部弹出
        window.setGravity(Gravity.BOTTOM);
        //获得窗体属性
        WindowManager.LayoutParams params = window.getAttributes();
        params.y = 0;
        window.setAttributes(params);
        dialog.show();
    }


    public static void showShareDialog(Context context, final IShareListener listener)
    {
        final Dialog dialog = new Dialog(context, R.style.ActionSheetDialogStyle);
        View view = LayoutInflater.from(context).inflate(R.layout.share_dialog, null);
        ImageView iv_close = (ImageView) view.findViewById(R.id.iv_share_dialog_close);
        iv_close.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                dialog.dismiss();
            }
        });

        ImageView iv_weixin = (ImageView) view.findViewById(R.id.iv_share_dialog_weixin);
        iv_weixin.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                listener.weixin();
            }
        });

        ImageView iv_friend = (ImageView) view.findViewById(R.id.iv_share_dialog_friend);
        iv_friend.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                listener.friend();
            }
        });

        ImageView iv_qq = (ImageView) view.findViewById(R.id.iv_share_dialog_qq);
        iv_qq.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                listener.qq();
            }
        });

        //布局给Dialog
        dialog.setContentView(view);
        //获取当前Activity窗体
        Window window = dialog.getWindow();
        //设置dialog 从底部弹出
        window.setGravity(Gravity.BOTTOM);
        window.getDecorView().setPadding(0, 0, 0, 0);
        //获得窗体属性
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.y = 0;
        window.setAttributes(params);
        dialog.show();
    }

    public static boolean cheeckLogin(final Context context)
    {
        return cheeckLogin(context, "");
    }

    public static boolean check(Context context)
    {
        if (UserManager.getInstance().getCurUser() != null)
        {
            return true;
        }
        UserInfo curUser = (UserInfo) SharedPreferencesUtis.getObjectFromShare(context, Config.USER_DATA_SAVE, Config.USER_DATA_SAVE);
        if (curUser != null)
        {
            UserManager.getInstance().setCurUser(curUser);
            return true;
        }
        return false;
    }

    public static boolean cheeckLogin(final Context context, final String tag)
    {
        if (check(context))
        {
            return true;
        }
        com.zgm.utils.UiUtils.showDialog(context, "登录提示", "您还未登录，立即登录？", "立即登录", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                Intent intent = new Intent(context, LoginActivity.class);
                intent.putExtra("tag", tag);
                context.startActivity(intent);
                if (!TextUtils.isEmpty(tag))
                {
                    MyApplication.finishActivity(context.getClass());
                }
                dialog.dismiss();
            }
        }, "取消", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                dialog.dismiss();
            }
        });
        return false;
    }


}
